package com.multi.mybatisplus.seata.controller;

import com.alibaba.fastjson.JSON;
import com.multi.mybatisplus.seata.mapper.order.OrderMapper;
import com.multi.mybatisplus.seata.pojo.BizConstant;
import com.multi.mybatisplus.seata.pojo.Order;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

/**
 * @author Administrator
 */
@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/order")
public class OrderController {
    private final OrderMapper orderMapper;

    /**
     * 添加订单
     *
     * @param orderId 订单id
     */
    @RequestMapping("/add/{orderId}/{goodsId}/{count}")
    public String orderAdd(@PathVariable("orderId") Long orderId,
                           @PathVariable("goodsId") Long goodsId,
                           @PathVariable("count") Integer count) {
        Order order = new Order();
        order.setOrderId(orderId);
        order.setGoodsId(goodsId);
        order.setCount(count);
        int orderResult = orderMapper.insertOneOrder(order);
        log.info("新增订单：{}，结果：{}", JSON.toJSONString(order), orderResult);
        if (orderResult > 0) {
            return BizConstant.SUCCESS;
        } else {
            return BizConstant.FAIL;
        }
    }

    /**
     * 订单详情
     *
     * @param orderId 订单id
     */
    @GetMapping("/info/{orderId}")
    public Order orderInfo(@PathVariable("orderId") Long orderId) {
        Order order = orderMapper.selectOneOrder(orderId);
        return order;
    }
}
